package sk.stuba.fiit.ltasys.web.mvc.model.entity;

import java.io.Serializable;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;


/**
 * User Entity.
 * 
 * @author Marek Hruska
 */
@Entity(name = "User")
@Table(name = "users")
public class UserEntity implements Serializable {

	public static final transient String USERID = "user_id";
	public static final transient String USERNAME = "username";
	
	private static final long serialVersionUID = -8712872385957386182L;

	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name ="user_id")
	private int userid;
	@Id
	private String username  = null;
	private String password  = null;
	private boolean enabled;
	@OneToMany()
	@LazyCollection(LazyCollectionOption.FALSE)
	@JoinColumn(name = "username")
	@Cascade({CascadeType.SAVE_UPDATE,CascadeType.DELETE})
	private List<AuthoritiesEntity> authority = null;
	
	
	private String organization = null;
	private Date dateOfBirth = null;
	private String city;
	private String country;
	private String street;
	private String telNumber;
	private String email;
	private String idCard;


	public UserEntity() {}
	
	public String calculatePassword(String password) throws NoSuchAlgorithmException{
		 MessageDigest sha1 = MessageDigest.getInstance("SHA-1");
		 sha1.update(password.getBytes());
		 String signature = new BigInteger(1,sha1.digest()).toString(16);
		 this.password = signature;
		 return signature;
	}


	public String getUsername() {
		return username;
	}


	public void setUsername(String username) {
		this.username = username;
	}


	public String getPassword() {
		return password;
	}


	public void setPassword(String password) {
		this.password = password;
	}


	public boolean isEnabled() {
		return enabled;
	}


	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}


	public List<AuthoritiesEntity> getAuthority() {
		return authority;
	}


	public void setAuthority(List<AuthoritiesEntity> authority) {
		this.authority = authority;
	}


	public String getOrganization() {
		return organization;
	}


	public void setOrganization(String organization) {
		this.organization = organization;
	}


	public Date getDateOfBirth() {
		return dateOfBirth;
	}


	public void setDateOfBirth(Date dateOfBirth) {
		this.dateOfBirth = dateOfBirth;
	}


	public String getCity() {
		return city;
	}


	public void setCity(String city) {
		this.city = city;
	}


	public String getCountry() {
		return country;
	}


	public void setCountry(String country) {
		this.country = country;
	}


	public String getStreet() {
		return street;
	}


	public void setStreet(String street) {
		this.street = street;
	}


	public String getTelNumber() {
		return telNumber;
	}


	public void setTelNumber(String telNumber) {
		this.telNumber = telNumber;
	}


	public String getEmail() {
		return email;
	}


	public void setEmail(String email) {
		this.email = email;
	}


	public String getIdCard() {
		return idCard;
	}


	public void setIdCard(String idCard) {
		this.idCard = idCard;
	}


	public int getUserid() {
		return userid;
	}


	public void setUserid(int userid) {
		this.userid = userid;
	}

}
